<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="年度">
        <div class="block">
          <span class="demonstration"></span>
          <el-date-picker
            v-model="queryParams.year"
            type="month"
            placeholder="请选择年月"
            value-format="YYYY-MM"
          />
        </div>
      </el-form-item>

      <el-form-item label="姓名">
        <el-input v-model="queryParams.name" placeholder="姓名"></el-input>
      </el-form-item>

      <el-form-item label="请选择车间" prop="workshopname">
        <el-select
          style="width: 200px"
          @change="handleQuery"
          v-model="queryParams.workshopname"
          placeholder="请选择车间"
          clearable
        >
          <el-option
            v-for="item in sh_workshop"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          ></el-option>
        </el-select>
      </el-form-item>

      <!--      <el-form-item label="机台" prop="machineId">-->
      <!--        <el-select filterable clearable v-model="queryParams.machineId">-->
      <!--          <el-option-->
      <!--              v-for="item in workbranchList"-->
      <!--              :label="item.machineName"-->
      <!--              :key="item.pid"-->
      <!--              :value="item.pid"-->
      <!--          ></el-option>-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->

      <!--      <el-form-item label="工艺" prop="producetechnics">-->
      <!--        <el-select v-model="queryParams.producetechnics"  placeholder="请选择">-->
      <!--          <el-option-->
      <!--              v-for="dict in ems_machine_info_type"-->
      <!--              :key="dict.value"-->
      <!--              :label="dict.label"-->
      <!--              :value="dict.value"-->
      <!--          ></el-option>-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->

      <!--      <el-form-item label="请选择车间" prop="workshopname">-->
      <!--        <el-select-->
      <!--            style="width: 200px"-->
      <!--            @change="handleQuery"-->
      <!--            v-model="queryParams.workshopname"-->
      <!--            placeholder="请选择车间"-->
      <!--        >-->
      <!--          <el-option-->
      <!--              v-for="item in sh_workshop"-->
      <!--              :key="item.value"-->
      <!--              :value="item.value"-->
      <!--              :label="item.label"-->
      <!--          ></el-option>-->

      <!--        </el-select>-->
      <!--      </el-form-item>-->

      <el-form-item>
        <!-- <el-button type="primary" >查询</el-button> -->
        <!-- <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button> -->

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Download" @click="handleExport"
          >导出</el-button
        >
      </el-col>
    </el-row>

    <el-table
      :data="tableData"
      v-loading="loading"
      height="900"
      border
      show-summary
      style="width: 100%"
    >
      <el-table-column
        fixed
        type="index"
        label="序号"
        width="70"
        align="center"
      >
      </el-table-column>
      <!--      <el-table-column  fixed prop="workshopname" label="车间" width="100" align="center"> -->
      <!--      </el-table-column>-->

      <el-table-column
        label="所属车间"
        min-width="100"
        align="center"
        prop="workshopname"
      >
        <template v-slot="{ row }">
          {{ row.workshopname }}
          <!-- {{ getshworkshop(row.workshopname) }} -->
        </template>
      </el-table-column>
      <!--      <el-table-column  fixed prop="machinename" label="机台" width="100" align="center"> </el-table-column>-->
      <el-table-column
        fixed
        prop="name"
        label="名字"
        width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="one" label="1" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.one > 0"
            type="primary"
            @click="handleView(row, day.one)"
            link
          >
            {{ row.one }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="two" label="2" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.two > 0"
            type="primary"
            @click="handleView(row, day.two)"
            link
          >
            {{ row.two }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="three" label="3" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.three > 0"
            type="primary"
            @click="handleView(row, day.three)"
            link
          >
            {{ row.three }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="four" label="4" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.four > 0"
            type="primary"
            @click="handleView(row, day.four)"
            link
          >
            {{ row.four }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="five" label="5" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.five > 0"
            type="primary"
            @click="handleView(row, day.five)"
            link
          >
            {{ row.five }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="six" label="6" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.six > 0"
            type="primary"
            @click="handleView(row, day.six)"
            link
          >
            {{ row.six }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="seven" label="7" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.seven > 0"
            type="primary"
            @click="handleView(row, day.seven)"
            link
          >
            {{ row.seven }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="eight" label="8" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.eight > 0"
            type="primary"
            @click="handleView(row, day.eight)"
            link
          >
            {{ row.eight }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="nine" label="9" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.nine > 0"
            type="primary"
            @click="handleView(row, day.nine)"
            link
          >
            {{ row.nine }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="ten" label="10" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.ten > 0"
            type="primary"
            @click="handleView(row, day.ten)"
            link
          >
            {{ row.ten }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="eleven" label="11" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.eleven > 0"
            type="primary"
            @click="handleView(row, day.eleven)"
            link
          >
            {{ row.eleven }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="twelve" label="12" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.twelve > 0"
            type="primary"
            @click="handleView(row, day.twelve)"
            link
          >
            {{ row.twelve }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="thirteen" label="13" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.thirteen > 0"
            type="primary"
            @click="handleView(row, day.thirteen)"
            link
          >
            {{ row.thirteen }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="fourteen" label="14" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.fourteen > 0"
            type="primary"
            @click="handleView(row, day.fourteen)"
            link
          >
            {{ row.fourteen }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="fifteen" label="15" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.fifteen > 0"
            type="primary"
            @click="handleView(row, day.fifteen)"
            link
          >
            {{ row.fifteen }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="sixteen" label="16" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.sixteen > 0"
            type="primary"
            @click="handleView(row, day.sixteen)"
            link
          >
            {{ row.sixteen }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="seventeen" label="17" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.seventeen > 0"
            type="primary"
            @click="handleView(row, day.seventeen)"
            link
          >
            {{ row.seventeen }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="eighteen" label="18" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.eighteen > 0"
            type="primary"
            @click="handleView(row, day.eighteen)"
            link
          >
            {{ row.eighteen }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="nineteen" label="19" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.nineteen > 0"
            type="primary"
            @click="handleView(row, day.nineteen)"
            link
          >
            {{ row.nineteen }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="twenty" label="20" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.twenty > 0"
            type="primary"
            @click="handleView(row, day.twenty)"
            link
          >
            {{ row.twenty }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="twentyone" label="21" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.twentyone > 0"
            type="primary"
            @click="handleView(row, day.twentyone)"
            link
          >
            {{ row.twentyone }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="twentytwo" label="22" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.twentytwo > 0"
            type="primary"
            @click="handleView(row, day.twentytwo)"
            link
          >
            {{ row.twentytwo }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="twentythree" label="23" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.twentythree > 0"
            type="primary"
            @click="handleView(row, day.twentythree)"
            link
          >
            {{ row.twentythree }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="twentyfour" label="24" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.twentyfour > 0"
            type="primary"
            @click="handleView(row, day.twentyfour)"
            link
          >
            {{ row.twentyfour }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="twentyfive" label="25" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.twentyfive > 0"
            type="primary"
            @click="handleView(row, day.twentyfive)"
            link
          >
            {{ row.twentyfive }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="twentysix" label="26" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.twentysix > 0"
            type="primary"
            @click="handleView(row, day.twentysix)"
            link
          >
            {{ row.twentysix }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="twentyseven" label="27" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.twentyseven > 0"
            type="primary"
            @click="handleView(row, day.twentyseven)"
            link
          >
            {{ row.twentyseven }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="twentyeight" label="28" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.twentyeight > 0"
            type="primary"
            @click="handleView(row, day.twentyeight)"
            link
          >
            {{ row.twentyeight }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="twentynine" label="29" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.twentynine > 0"
            type="primary"
            @click="handleView(row, day.twentynine)"
            link
          >
            {{ row.twentynine }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column prop="thirty" label="30" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.thirty > 0"
            type="primary"
            @click="handleView(row, day.thirty)"
            link
          >
            {{ row.thirty }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="thirtyone" label="31" width="80" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.thirtyone > 0"
            type="primary"
            @click="handleView(row, day.thirtyone)"
            link
          >
            {{ row.thirtyone }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column
        fixed="right"
        prop="total"
        label="合计"
        width="100"
        align="center"
      ></el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Printdailywagestatistics">
import { ref, onMounted } from "vue";
import {
  exportprint,
  exportUseListhj,
  getPrintdailyList,
} from "@/api/repord/userrepord";
import dayjs from "dayjs";
import { downLoadBlob } from "@/utils/ruoyi.js";
import {
  getList as getWorkBenchList,
  getMachine,
} from "@/api/basicInfo/workbench.js";
// import { format } from 'date-fns';

const router = useRouter();
const { proxy } = getCurrentInstance();
const { ems_machine_info_type, sh_workshop } = proxy.useDict(
  "ems_machine_info_type",
  "sh_workshop"
);

const tableData = ref([]);
const total = ref(0);
const loading = ref(true);
const defaultYearMonth = ref(dayjs().format("YYYY-MM"));
const workbranchList = ref([]);
const queryParams = ref({
  pageNum: 1,
  pageSize: 200,
  workshopname: undefined,
  // yearmonth: defaultYearMonth,
  year: defaultYearMonth,
  // month: defaultYearMonth,
  producetechnics: "2",
  name: undefined,
});
const day = ref({
  one: "01",
  two: "02",
  three: "03",
  four: "04",
  five: "05",
  six: "06",
  seven: "07",
  eight: "08",
  nine: "09",
  ten: "10",
  eleven: "11",
  twelve: "12",
  thirteen: "13",
  fourteen: "14",
  fifteen: "15",
  sixteen: "16",
  seventeen: "17",
  eighteen: "18",
  nineteen: "19",
  twenty: "20",
  twentyone: "21",
  twentytwo: "22",
  twentythree: "23",
  twentyfour: "24",
  twentyfive: "25",
  twentysix: "26",
  twentyseven: "27",
  twentyeight: "28",
  twentynine: "29",
  thirty: "30",
  thirtyone: "31",
});

getMachine().then((res) => {
  res.rows.forEach((item) => {
    //item.item.machineId = item.pid;
    item.machineName = item.machineName + "/" + item.workshopName;
  });
  workbranchList.value = res.rows;
  if (res.rows?.length > 0) {
    queryParams.value.machineId = res.rows[0].pid;
  } else {
    getWorkBenchList({ pageNum: 1, pageSize: 100, type: "2" }).then((resp) => {
      resp.rows.forEach((item) => {
        //item.machineId = item.pid;
        item.machineName = item.machineName + "/" + item.workshopName;
      });
      workbranchList.value = resp.rows;
    });
  }
  getList();
});

// 获取列表
const getList = async () => {
  try {
    loading.value = true;
    // queryParams.value.year=queryParams.value.year.slice(0,4)
    // queryParams.value.month=queryParams.value.month.slice(5,7)
    const response = await getPrintdailyList(queryParams.value);
    tableData.value = response.rows;
    total.value = response.total;
    loading.value = false;
  } catch (error) {
    console.error(error);
    // 处理错误逻辑
  }
};

// 在组件挂载后获取列表
onMounted(getList);

const getshworkshop = (row) => {
  const val = sh_workshop.value.find((item) => item.value == row);
  return val ? val.label : "";
};

// 查看
const handleView = (row, value) => {
  router.push({
    path: "/repord/persion/details",
    query: {
      name: row.name,
      day: value,
      year: queryParams.value.year.slice(0, 4),
      month: queryParams.value.year.slice(5, 7),
      info: true,
    },
  });
};

// 搜索按钮操作
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value.name = undefined;
  queryParams.value.machineId = undefined;
  queryParams.value.workshopname = undefined;
}

// 导出工资合计
const handleExport = () => {
  exportprint(queryParams.value).then((res) => {
    downLoadBlob(res, "工资明细");
  });
};
</script>